/*
 * 消息组件 Message
 * 依赖Message DPL
 * author:yanmu.wj@taobao.com
 * date:2011-11-22
 */
KISSY.add('Message',function(S){
    var $ = S.all;
    var Message = function(msg){
        this.wrapper = S.one(msg);
        this.msg = this.wrapper && this.wrapper.one('.msg-default'),
        this.title = this.wrapper && this.wrapper.one('.msg-title'),
        this.content = this.wrapper && this.wrapper.one('.msg-content');

        this.isWeak = this.wrapper && this.wrapper.hasClass('msg-weak');
    };
    var _Message = function(msg){
        return new Message(msg);
    };
    S.augment(Message,{
        change:function(type,cfg){
            if(!this.msg) return;

            //本身非weak模式，更新时移出weak模式
            if(!this.isWeak) this.wrapper.removeClass('msg-weak');

            this.show();

            var type = type && type.toUpperCase() || '';
            switch(type){
                case 'OK':
                    this.msg.attr('class','msg-default msg-ok');
                    break;
                case 'ERROR':
                    this.msg.attr('class','msg-default msg-error');
                    break;
                case 'TIPS':
                    this.msg.attr('class','msg-default msg-tips');
                    break;
                case 'NOTICE':
                    this.msg.attr('class','msg-default msg-notice');
                    break;
                case 'ATTENTION':
                    this.msg.attr('class','msg-default msg-attention');
                    break;
                case 'QUESTION':
                    this.msg.attr('class','msg-default msg-question');
                    break;
                case 'STOP':
                    this.msg.attr('class','msg-default msg-stop');
                    break;
                default:break;
            }

            this._changeText(cfg);
        },
        _changeTitle:function(s){
            if(!this.title || !S.isString(s)) return;
            this.title.html(s);
        },
        _changeContent:function(s){
            if(!this.content || !S.isString(s)) return;
            this.content.html(s);
        },
        _changeText:function(cfg){
            /*
             * 如果传入对象 title = cfg.title,content = cfg.content
             * 如果传入字符串 content = cfg
             */
            var _title = S.isObject(cfg) && S.isString(cfg.title) ? cfg.title : '',
                _content = S.isObject(cfg) && S.isString(cfg.content) ? cfg.content : (S.isString(cfg) ? cfg : '');

            this._changeTitle(_title);
            this._changeContent(_content);

            //如果没有内容切换到weak模式
            if(!_title && !_content && this.wrapper){
                this.wrapper.addClass('msg-weak');
            }
        },
        ok:function(cfg){
            this.change('ok',cfg);
        },
        error:function(cfg){
            this.change('error',cfg);
        },
        tips:function(cfg){
            this.change('tips',cfg);
        },
        notice:function(cfg){
            this.change('notice',cfg);
        },
        attention:function(cfg){
            this.change('attention',cfg);
        },
        question:function(cfg){
            this.change('question',cfg);
        },
        stop:function(cfg){
            this.change('stop',cfg);
        },
        hide:function(){
            this.wrapper.css('visibility','hidden');
        },
        show:function(){
            this.wrapper.css('visibility','visible');
        }
    })

    return _Message;
});
